package dbtest;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Henrik
 */
public class Tile extends Component {
    public int row;
    public int col;
    private Piece piece;
    private BufferedImage img;
    
    public Tile(int x, int y) {
        super();
        setLocation(x, y);
        row = y / 64;
        col = x / 64;
        if (col % 2 == 0 && row % 2 == 0 || col % 2 == 1 && row % 2 == 1) {
            placeWhiteTile();
        }
        if (col % 2 == 1 && row % 2 == 0 || col % 2 == 0 && row % 2 == 1) {
            placeBlackTile();
        }
    }

    private void placeWhiteTile() {
        try {
            img = ImageIO.read(new File("tiles" + File.separator + "w_tile.png"));
        } catch (IOException ex) {
            Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void placeBlackTile() {
        try {
            img = ImageIO.read(new File("tiles" + File.separator + "b_tile.png"));
        } catch (IOException ex) {
            Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void setPiece(Piece piece) {
        this.piece = piece;
    }
    
    public Piece getPiece() {
        return piece;
    }
    
    public BufferedImage getImage() {
        return img;
    }
    
    @Override
    public void paint(Graphics g) {
        //System.out.println("paint tile: " + getX() + " " + getY());
        Graphics2D g2d = (Graphics2D) g;
//        g.setColor(Color.RED);
//        g.fillRect(0, 0, 64, 64);
        g.drawImage(img, getX(), getY(), this);
        if (piece != null) {
            g.drawImage(piece.getImage(), getX(), getY(), this);
        }
    }
}
